package CCRMClient.GUI;

import javax.swing.JPanel;

import java.awt.Color;

import javax.swing.border.LineBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JTextArea;

import java.awt.SystemColor;

import javax.swing.JTextField;
import javax.swing.JButton;

import CCRMClient.ClientStart;
import CCRMClient.Controllers.CatalogManager;
import Common.SERVER_COMMANDS;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

/**
 * This class implements the add category UC screen panel
 * @author Ori Ziv
 *
 */
public class AddCategoryScreen extends JPanel {
	
	private JLabel lblAddNewCategory;
	private JLabel lblCategoryName;
	private JLabel lblDescription;
	private JTextArea textAreaDscription;
	private JTextField textCategoryName;
	private JButton btnSubmitNewCategory;
	private JButton btnCancelAction;

	/**
	 * Create the panel.
	 */
	public AddCategoryScreen() {
	
		initialize();
	}
	private void initialize() {
		setSize(450,330);
		setBorder(new LineBorder(new Color(0, 0, 0)));
		setBackground(Color.WHITE);
		setLayout(null);
		add(getLblAddNewCategory());
		add(getLblCategoryName());
		add(getLblDescription());
		add(getTextAreaDscription());
		add(getTextCategoryName());
		add(getBtnSubmitNewCategory());
		add(getBtnCancelAction());
	}
	JLabel getLblAddNewCategory() {
		if (lblAddNewCategory == null) {
			lblAddNewCategory = new JLabel("Add New Category");
			lblAddNewCategory.setHorizontalAlignment(SwingConstants.CENTER);
			lblAddNewCategory.setForeground(Color.RED);
			lblAddNewCategory.setFont(new Font("David", Font.BOLD, 26));
			lblAddNewCategory.setBackground(Color.WHITE);
			lblAddNewCategory.setBounds(0, 0, 450, 49);
		}
		return lblAddNewCategory;
	}
	JLabel getLblCategoryName() {
		if (lblCategoryName == null) {
			lblCategoryName = new JLabel("Category Name:");
			lblCategoryName.setFont(new Font("David", Font.BOLD, 20));
			lblCategoryName.setBounds(10, 60, 164, 29);
		}
		return lblCategoryName;
	}
	JLabel getLblDescription() {
		if (lblDescription == null) {
			lblDescription = new JLabel("Description:");
			lblDescription.setFont(new Font("David", Font.BOLD, 20));
			lblDescription.setBounds(10, 111, 164, 29);
		}
		return lblDescription;
	}
	JTextArea getTextAreaDscription() {
		if (textAreaDscription == null) {
			textAreaDscription = new JTextArea();
			textAreaDscription.setBorder(new LineBorder(new Color(0, 0, 0)));
			textAreaDscription.setBackground(SystemColor.control);
			textAreaDscription.setFont(new Font("Courier New", Font.PLAIN, 11));
			textAreaDscription.setLineWrap(true);
			textAreaDscription.setBounds(10, 151, 412, 77);
		}
		return textAreaDscription;
	}
	JTextField getTextCategoryName() {
		if (textCategoryName == null) {
			textCategoryName = new JTextField();
			textCategoryName.setBackground(SystemColor.control);
			textCategoryName.setBounds(171, 65, 251, 20);
			textCategoryName.setColumns(10);
		}
		return textCategoryName;
	}
	JButton getBtnSubmitNewCategory() {
		if (btnSubmitNewCategory == null) {
			btnSubmitNewCategory = new JButton("Submit New Category");
			btnSubmitNewCategory.addActionListener(new BtnSubmitNewCategoryActionListener());
			btnSubmitNewCategory.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnSubmitNewCategory.setBounds(10, 253, 195, 23);
		}
		return btnSubmitNewCategory;
	}
	JButton getBtnCancelAction() {
		if (btnCancelAction == null) {
			btnCancelAction = new JButton("Cancel Action");
			btnCancelAction.addActionListener(new BtnCancelActionActionListener());
			btnCancelAction.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnCancelAction.setBounds(298, 253, 124, 23);
		}
		return btnCancelAction;
	}
	/**
	 * This inner class implements event listener for pressing Cancel Action button
	 * @author Ori Ziv
	 *
	 */
	private class BtnCancelActionActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			textCategoryName.setText("");
			textAreaDscription.setText("");
			CatalogScreen catScrn = new CatalogScreen();
			setVisible(false);
			ClientStart.panelSwicher(catScrn);
		}
	}
	/**
	 * This inner class implements event listener for pressing Submit New Category button
	 * @author Ori Ziv
	 *
	 */
	private class BtnSubmitNewCategoryActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			String catName;
			String catDescription;
			try{
				catName = textCategoryName.getText();
				catDescription = textAreaDscription.getText();
				if(catName.isEmpty()||catDescription.isEmpty() ) throw new NullPointerException();
				try {
					ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.ADD_CATEGORY, CatalogManager.addCategory(catName, catDescription));
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
					ClientStart.quitApp();
				} // end of try catch IOException
		} catch (NullPointerException e) {
				JOptionPane.showMessageDialog(null, "Both fields are mandatory", "Add category problem", JOptionPane.INFORMATION_MESSAGE);
				textCategoryName.setText("");
				textAreaDscription.setText("");
			} // end of try catch NullPointerException
		} // end of method
	} // end of inner class
}
